import 'package:flutter/material.dart';
import 'package:get/get.dart';

class HomeWidget extends StatelessWidget {
  final Function openSettings;
  final Function openTasks;
  final Function openFact;
  final Function openReports;

  HomeWidget({
    Key? key,
    required this.openFact,
    required this.openTasks,
    required this.openReports,
    required this.openSettings,
  }) : super(key: key);

  final _scaffoldKey = GlobalKey<ScaffoldState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      appBar: AppBar(
        centerTitle: true,
        title: const Text('AgroСклад'),
        elevation: 0,
        actions: [
          IconButton(
            icon: const Icon(Icons.menu),
            onPressed: () {
              _scaffoldKey.currentState!.openEndDrawer();
            },
          ),
        ],
      ),
      body: Container(
        decoration: const BoxDecoration(
          color: Colors.indigo,
        ),
        child: Container(
          decoration: const BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.vertical(
              top: Radius.circular(20),
            ),
          ),
          child: ListView(
            primary: false,
            padding: const EdgeInsets.all(20),
            children: [
              Container(
                height: 150,
                margin: const EdgeInsets.only(bottom: 20),
                decoration: BoxDecoration(
                  color: const Color(0xff3EB068),
                  borderRadius: BorderRadius.circular(20),
                  boxShadow: [
                    BoxShadow(
                      color: const Color(0xff3EB068).withOpacity(0.5),
                      spreadRadius: 0,
                      blurRadius: 5,
                      offset: const Offset(0, 3), // changes position of shadow
                    ),
                  ],
                ),
                child: InkWell(
                  onTap: () => openFact(),
                  child: Ink(
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: const [
                        Icon(
                          Icons.add,
                          color: Colors.white,
                          size: 60.0,
                        ),
                        Text(
                          "Cоздать документ",
                          style: TextStyle(
                            fontSize: 25,
                            color: Colors.white,
                            fontWeight: FontWeight.w400,
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ),

              // Документы
              Container(
                margin: const EdgeInsets.only(bottom: 20),
                height: 150,
                decoration: BoxDecoration(
                  color: const Color(0xffffa000),
                  borderRadius: BorderRadius.circular(20),
                  boxShadow: [
                    BoxShadow(
                      color: const Color(0xff3EB068).withOpacity(0.5),
                      spreadRadius: 0,
                      blurRadius: 5,
                      offset: const Offset(0, 3), // changes position of shadow
                    ),
                  ],
                ),
                child: InkWell(
                  onTap: () => openTasks(),
                  child: Ink(
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: const [
                        Icon(
                          Icons.playlist_add_check,
                          color: Colors.white,
                          size: 60.0,
                        ),
                        Text("Документы",
                            style: TextStyle(
                              fontSize: 25,
                              color: Colors.white,
                              fontWeight: FontWeight.w400,
                            )),
                      ],
                    ),
                  ),
                ),
              ),
              // Отчеты
              Container(
                margin: const EdgeInsets.only(bottom: 20),
                height: 150,
                decoration: BoxDecoration(
                  color: const Color(0xffF46768),
                  borderRadius: BorderRadius.circular(20),
                  boxShadow: [
                    BoxShadow(
                      color: const Color(0xffF46768).withOpacity(0.5),
                      spreadRadius: 0,
                      blurRadius: 5,
                      offset: const Offset(0, 3), // changes position of shadow
                    ),
                  ],
                ),
                child: InkWell(
                  onTap: () => openReports(),
                  child: Ink(
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: const [
                        Icon(
                          Icons.source_outlined,
                          color: Colors.white,
                          size: 60.0,
                        ),
                        Text("Отчеты",
                            style: TextStyle(
                              fontSize: 25,
                              color: Colors.white,
                              fontWeight: FontWeight.w400,
                            )),
                      ],
                    ),
                  ),
                ),
              ),
              // Настройки
              Container(
                margin: const EdgeInsets.only(bottom: 20),
                height: 150,
                decoration: BoxDecoration(
                  color: const Color(0xff5B69FB),
                  borderRadius: BorderRadius.circular(20),
                  boxShadow: [
                    BoxShadow(
                      color: const Color(0xff5B69FB).withOpacity(0.5),
                      spreadRadius: 0,
                      blurRadius: 5,
                      offset: const Offset(0, 3), // changes position of shadow
                    ),
                  ],
                ),
                child: InkWell(
                  onTap: () => openSettings(),
                  child: Ink(
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: const [
                        Icon(
                          Icons.settings,
                          color: Colors.white,
                          size: 60.0,
                        ),
                        Text("Настройки",
                            style: TextStyle(
                              fontSize: 25,
                              color: Colors.white,
                              fontWeight: FontWeight.w400,
                            )),
                      ],
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
      endDrawer: Drawer(
        child: Stack(
          children: [
            ListView(
              padding: const EdgeInsets.only(bottom: 70),
              children: [
                UserAccountsDrawerHeader(
                  currentAccountPicture: const CircleAvatar(
                    backgroundColor: Colors.white,
                    child: Icon(Icons.person, size: 40.0),
                  ),
                  otherAccountsPictures: [
                    IconButton(
                      icon: const Icon(Icons.exit_to_app),
                      onPressed: () => Get.back(),
                      color: Colors.white,
                    ),
                  ],
                  accountEmail: null,
                  accountName: null,
                ),
                ListTile(
                  leading: const Icon(Icons.playlist_add_check),
                  title: const Text('Документы', style: TextStyle(fontSize: 16.0)),
                  onTap: () => openTasks(),
                ),
                ListTile(
                  leading: const Icon(Icons.source_outlined),
                  title: const Text('Отчеты', style: TextStyle(fontSize: 16.0)),
                  onTap: () => openReports(),
                ),
                ListTile(
                  leading: const Icon(Icons.settings),
                  title: const Text('Настройки', style: TextStyle(fontSize: 16.0)),
                  onTap: () => openSettings(),
                ),
                ListTile(
                  leading: const Icon(Icons.close),
                  title: const Text('Закрыть', style: TextStyle(fontSize: 16.0)),
                  onTap: () => Get.back(),
                ),
              ],
            ),
            Positioned(
                left: 0.0,
                right: 0.0,
                bottom: 0.0,
                child: TextButton(
                  style: TextButton.styleFrom(
                    padding: const EdgeInsets.all(15),
                    primary: Colors.white,
                    backgroundColor: Colors.indigo,
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(0)),
                  ),
                  onPressed: () async {
                    bool pop = true;
                    Future.delayed(const Duration(seconds: 1), () async {
                      if (pop) Get.back();
                    });
                    await Get.toNamed('/loading');
                    pop = false;
                  },
                  child: Row(
                    children: [
                      const Icon(
                        Icons.autorenew,
                        color: Colors.white,
                        size: 25.0,
                      ),
                      Container(
                        padding: const EdgeInsets.only(left: 22.0),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: const [
                            Text(
                              '',
                              // dbVersion != null && dbVersion!.isNotEmpty ? 'Синхронизация выполнена' : 'Нет синхронизации',
                              style: TextStyle(
                                fontSize: 12.0,
                                fontWeight: FontWeight.w500,
                                color: Colors.white,
                              ),
                              textAlign: TextAlign.left,
                            ),
                            Text(
                              '',
                              // dbVersion != null && dbVersion!.length == 19
                              //     ? '${dbVersion!.substring(8, 10)}.${dbVersion!.substring(5, 7)}.${dbVersion!.substring(0, 4)} ${dbVersion!.substring(11)}'
                              //     : (dbVersion ?? ''),
                              style: TextStyle(
                                fontSize: 14.0,
                                fontWeight: FontWeight.w300,
                                color: Colors.white,
                              ),
                              textAlign: TextAlign.left,
                              softWrap: true,
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                )),
          ],
        ),
      ),
    );
  }
}
